<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>商城管理</el-breadcrumb-item>
      <el-breadcrumb-item v-if="fromUrl==1" :to="{ path: '/commodity' }"
        >商城列表</el-breadcrumb-item
      >
      <el-breadcrumb-item v-if="fromUrl==2" :to="{ path: '/commodityType' }"
        >商城分类</el-breadcrumb-item
      >
      <el-breadcrumb-item v-if="fromUrl==2" :to="{ path: '/commodityType/moreCommodityType/'+ this.$route.params.typeId}"
        >分类详情</el-breadcrumb-item
      >
      <el-breadcrumb-item>商品详情</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <div class="form_title">
        <h5>商品详情</h5>
      </div>
      <!-- 商品详情表单 -->
      <el-form
        ref="form"
        :model="form"
        label-width="95px"
        disabled="true"
      >
        <el-form-item label="名称：" prop="name">
          <el-col :span="11">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item class="imgFrom" label="上传图片：">
          <el-image
            class="left"
            style="width: 100px; height: 100px"
            :src="$global.imgURL + form.image"
          >
          </el-image>
        </el-form-item>
        <el-form-item label="分类：">
          <el-col :span="12">
            <el-select
              v-model="form.typeId"
              filterable
              placeholder="请选择"
              value-key="id"
              style="width: 100%"
            >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="口味：">
          <el-col :span="12">
            <el-select
              filterable
              multiple
              v-model="form.flavorId"
              placeholder="请选择"
              value-key="id"
              style="width: 100%"
            >
              <el-option
                v-for="item in flavorList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="质量：" prop="quality">
          <el-col :span="11">
            <el-input v-model="form.quality"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="价格：" prop="price">
          <el-col :span="11">
            <el-input v-model="form.price">
              <template slot="append">元</template>
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="介绍：" prop="describe">
          <el-col :span="11">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.describe"
            >
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item class="state" label="状态：" prop="state">
          <div class="stateRadio">
            <el-radio v-model="form.state" label="1">启用</el-radio>
            <el-radio v-model="form.state" label="2">禁用</el-radio>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      labelPosition: "right",
      typeList: [],
      flavorList: [],
      form: {
        name: "",
        image: "",
        memberId: "",
        price: "",
        state: "1",
        quality: "",
        typeId: "",
        flavorId: [],
        describe: "",
      },
      fromUrl:""
    };
  },
  methods: {
    //得到商品分类列表
    async getTypes() {
      const { data: res } = await this.$http.get(
        "commodityType/getCommodityInState"
      );
      console.log(res);
      this.typeList = res.data.types;
      return;
    },
    //得到商品口味列表
    async getFlavors() {
      const { data: res } = await this.$http.get("Flavor/listFlavor");
      console.log(res);
      this.flavorList = res.data.flavors;
      return;
    },
    //得到此id的商品信息
    async getCommodityById(id) {
      const { data: res } = await this.$http.get(
        "commodity/getCommodityById?id=" + id
      );
      console.log("========================");
      console.log(res);
      console.log(res.data.flavorId);
      this.form.flavorId = res.data.flavorId;
      this.form.name = res.data.commodity.name;
      this.form.typeId = res.data.commodity.typeId;
      this.form.price = res.data.commodity.price / 100;
      this.form.quality = res.data.commodity.quality;
      this.form.describe = res.data.commodity.describe;
      this.form.image = res.data.commodity.image;
      return;
    },
  },
  created() {
    this.getTypes();
    this.getFlavors();
    this.getCommodityById(this.$route.params.id);
    console.log(this.$route.params.fromUrl);
    this.fromUrl = this.$route.params.fromUrl;
  },
};
</script>

<style lang="less" scoped>
.form_title {
  height: 40px;
  text-align: left;
  line-height: 0px;
  letter-spacing: 1px;
  border-left: rgba(64, 158, 255, 0.781) solid 3px;
  box-shadow: 0 3px 8px #ddd;
  padding-left: 10px;
  margin-top: 0px;
  overflow: hidden;
}

.el-form {
  width: 800px;
  margin: 50px auto;
  text-align: center;
  padding-left: 350px;
}
.formbutton,
.state {
  position: relative;
}
.formbutton .formsubmit,
.state .stateRadio {
  position: absolute;
  left: 80px;
}
.imgFrom {
  display: flex;
}
</style>
